Long Short-Term Memory (LSTM) Example

This is a simple example of Long Short-Term Memory (LSTM) using Python and the TensorFlow/Keras libraries.

LSTM Overview

Long Short-Term Memory (LSTM) is a type of recurrent neural network (RNN) architecture designed to capture long-range dependencies in sequential data. It addresses the vanishing gradient problem of traditional RNNs by introducing memory cells with gating mechanisms. LSTMs are widely used for various sequence-based tasks such as time series prediction, natural language processing, and speech recognition.

Key concepts of LSTMs:

Python Source Code:

# Import necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense

# Generate synthetic time series data
def generate_time_series(n):
    t = np.linspace(0.0, 1.0, n)
    series = 5 * np.sin(10 * np.pi * t) + 0.1 * np.random.randn(n)
    return series

# Create a synthetic time series dataset
n_samples = 1000
time_series = generate_time_series(n_samples)

# Prepare data for training an LSTM
n_steps = 50
X, y = [], []
for i in range(n_samples - n_steps):
    X.append(time_series[i:i+n_steps])
    y.append(time_series[i+n_steps])

X = np.array(X).reshape(-1, n_steps, 1)
y = np.array(y)

# Build an LSTM model
model = Sequential([
    LSTM(20, activation='relu', input_shape=(n_steps, 1)),
    Dense(1)
])

model.compile(optimizer='adam', loss='mse')

# Train the LSTM model
model.fit(X, y, epochs=20)

# Generate predictions on new data
X_new = time_series[-n_steps:].reshape(1, n_steps, 1)
y_pred = model.predict(X_new)

# Plot the original and predicted time series
plt.figure(figsize=(10, 6))
plt.plot(np.arange(n_samples), time_series, label='Original Time Series', linewidth=2)
plt.plot(np.arange(n_samples, n_samples + n_steps), y_pred[0], label='Predicted Values', linestyle='dashed', linewidth=2)
plt.title('LSTM Time Series Prediction')
plt.xlabel('Time Steps')
plt.ylabel('Values')
plt.legend()
plt.show()

Explanation: